package com.csgc.camera.http.repository

import com.csgc.camera.bean.AppInitInfo
import com.csgc.camera.bean.BindPhoneParams
import com.csgc.camera.bean.CodeLoginParams
import com.csgc.camera.bean.FeedBackParams
import com.csgc.camera.bean.InitParams
import com.csgc.camera.bean.OaidParams
import com.csgc.camera.bean.SendCodeParams
import com.csgc.camera.bean.UserInfo
import com.csgc.camera.bean.UserInfoParams
import com.csgc.camera.bean.VipPayParams
import com.csgc.camera.bean.VipType
import com.csgc.camera.bean.VipTypeParams
import com.csgc.camera.bean.WxBindParams
import com.csgc.camera.bean.WxLoginParams
import com.csgc.camera.bean.WxPayInfoData
import com.csgc.camera.http.ApiService
import com.csgc.camera.http.BaseResponse
import com.csgc.camera.http.ResponseResult
import com.csgc.camera.http.api.AppApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn

class AppRepository {
    private val api:AppApi by lazy {
        ApiService.createService(AppApi::class.java)
    }


    fun loginByCode(param: CodeLoginParams): Flow<ResponseResult<BaseResponse<UserInfo>>> {
        return flow{
            try {
                val response = api.loginByCode(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }

    fun getUserInfo(param: UserInfoParams): Flow<ResponseResult<BaseResponse<UserInfo>>> {
        return flow{
            try {
                val response = api.getUserInfo(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }


    /**
     * 注销用户
     */
    fun cleanUserInfo(param: UserInfoParams): Flow<ResponseResult<BaseResponse<Any>>> {
        return flow{
            try {
                val response = api.cleanUserInfo(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }

    /**
     * 发送验证码
     */
    fun sendSmsCode(param: SendCodeParams): Flow<ResponseResult<BaseResponse<Any>>> {
        return flow{
            try {
                val response = api.sendSmsCode(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }


    /**
     * 获取vip类型列表
     */
    fun getVipType(param: VipTypeParams): Flow<ResponseResult<BaseResponse<List<VipType>>>> {
        return flow{
            try {
                val response = api.getVipTypeList(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }

    /**
     * 支付宝支付信息
     */
    fun getAliPayInfo(param: VipPayParams): Flow<ResponseResult<BaseResponse<String>>> {
        return flow{
            try {
                val response = api.getAliPayInfo(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }


    /**
     * 反馈
     */
    fun submitFeedBack(param: FeedBackParams): Flow<ResponseResult<BaseResponse<Any>>> {
        return flow{
            try {
                val response = api.submitFeedBack(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }


    /**
     * 微信登录
     */
    fun loginByWx(param: WxLoginParams): Flow<ResponseResult<BaseResponse<UserInfo>>> {
        return flow{
            try {
                val response = api.loginByWx(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }

    /**
     * 微信绑定
     */
    fun bindWx(param: WxBindParams): Flow<ResponseResult<BaseResponse<UserInfo>>> {
        return flow{
            try {
                val response = api.bindWx(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }


    /**
     * 手机号绑定
     */
    fun bindPhone(param: BindPhoneParams): Flow<ResponseResult<BaseResponse<UserInfo>>> {
        return flow{
            try {
                val response = api.bindPhone(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }




    /**
     * 微信支付信息
     */
    fun getWxPayInfo(param: VipPayParams): Flow<ResponseResult<BaseResponse<WxPayInfoData>>> {
        return flow{
            try {
                val response = api.getWxPayInfo(param)

                if (response.isSuccess()){
                    emit(ResponseResult.Success(response))
                }else{
                    emit(ResponseResult.Failure(Exception(response.msg)))
                }
            }catch (e:Exception){
                emit(ResponseResult.Failure(e))
            }

        }.catch {
            emit(ResponseResult.Failure(Exception(it.message)))
        }
            .flowOn(Dispatchers.IO)

    }

}